﻿$(document).ready(function () {
    var interval = 3000;

    var _buttons = $('ul.pagination').children('li');
    var _banners = $('div.slides_control').children('div');
    var _currentIndex = 1;

    _buttons.children('a').each(function (index, element) {
        $(this).click(function () {
            _currentIndex = index;
            run();
            reSetRun();
        });

    });

    function changeCurrentbanner(banner, button, banners, buttons) {
        banners.each(function () {
            $(this).css('display', 'none');
        });

        buttons.each(function () {
            $(this).removeClass('current');
        });

        $(banner).css('display', 'block');
        $(button).addClass('current');
    }

    function run() {
        if (_currentIndex > 3)
            _currentIndex = 0;
        changeCurrentbanner(_banners[_currentIndex], _buttons[_currentIndex], _banners, _buttons);
        _currentIndex++;
    }

    var setIntervalId;

    function reSetRun()
    {
        clearInterval(setIntervalId);
        setIntervalId = setInterval(run, interval);
    }
    reSetRun();
  
});